<template>
  <div>
    <el-dialog
      title="编辑员工"
      :visible="isShow"
      width="50%"
      @close="closeEvent"
    >
      <el-form label-width="120px" :model="form" :rules="rules" ref="form">
        <el-form-item label="姓名" prop="username">
          <el-input placeholder="1~3个字符" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            type="date"
            v-model="form.timeOfEntry"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形势" prop="formOfEmployment">
          <el-select placeholder="请输入" v-model="form.formOfEmployment">
            <el-option
              v-for="item in employees.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div ref="treeArea">
            <el-input
              readonly
              v-model="form.departmentName"
              placeholder="请输入"
              @focus="focusEvent"
            ></el-input>
            <el-tree
              :data="treeData"
              v-if="showTree"
              @node-click="nodeClick"
              :props="{ label: 'name' }"
            >
            </el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            type="date"
            v-model="form.correctionTime"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="footer">
          <el-button type="primary" @click="submit">确 定</el-button>
          <el-button>取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { companyDepartment } from '@/api/departments.js'
import { sysUserPost } from '@/api/employees.js'
import employees from '@/assets/constant/employees.js'
export default {
  name: 'dialogItem',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      showTree: false,
      treeData: [],
      employees: employees,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		手机号
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转[{required:true,message:'必填',trigger:'change'}],
      }
    }
  },
  mounted () {
    document.addEventListener('click', this.cilckEvent)
  },
  methods: {
    closeEvent () {
      this.$emit('update:isShow', false)
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('添加成功')
          this.closeEvent()
          //刷新父级列表数据
          this.$emit('getDate')
          console.log(result)
        }
      })
    },
    async focusEvent () {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log(res)
      }
    },
    changeData (arr, str = '') {
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    //树形组件点击
    nodeClick (data) {
      this.form.departmentName = data.name
      //   console.log(data)
    },
    //点击区域外关闭
    cilckEvent (e) {
      if (this.isShow === true && !this.$refs.treeArea.contains(e.target)) {
        this.showTree = false
      }
    },
    beforeDestroy () {
      document.removeEventListener('click', this.cilckEvent)
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
